package com.woniu.adapter;

import com.woniu.outlet.dao.MessagePoMapper;
import com.woniu.outlet.dao.PurchaseDetailPoMapper;
import com.woniu.outlet.dao.PurchasePoMapper;
import com.woniu.outlet.po.*;
import com.woniu.uitl.MessageStatus;
import com.woniu.uitl.PurchaseStatus;
import com.woniu.uitl.SnowflakeIdGenerator;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 采购单适配层
 */
@Component
public class PurchaseAdapter {

    @Autowired
    private PurchasePoMapper purchasePoMapper;
    @Autowired
    private MessagePoMapper messagePoMapper;
    @Autowired
    private PurchaseDetailPoMapper purchaseDetailPoMapper;

    //添加采购单
    public void addPurchasePo(PurchasePo po){
        //补全信息
        SnowflakeIdGenerator idWorker = new SnowflakeIdGenerator(1, 1);
        long id = idWorker.nextId();
        long purDetailId = idWorker.nextId();
        long purId = idWorker.nextId();
        po.setId(id);
        po.setPurchaseNo(purId);
        po.setStatus(PurchaseStatus.UNSUBMITTED.getStatus());
        po.setCreateTime(new Date());
        po.setTrueTotal(new BigDecimal(0));
        purchasePoMapper.insertSelective(po);
    }

    //修改采购单
    public void updatePurchase(PurchasePo po){
        po.setFixTime(new Date());
        purchasePoMapper.updateByPrimaryKeySelective(po);
    }

    //删除采购单
    public String deletePurchase(Long id){
        PurchasePo purchasePo = purchasePoMapper.selectByPrimaryKey(id);
        if (!purchasePo.getStatus().equals(PurchaseStatus.UNSUBMITTED.getStatus())){
            return "该采购单不支持本次操作";
        }
        try {
            purchasePoMapper.deleteByPrimaryKey(id);
            //删除采购详情表中的数据
            PurchaseDetailPoExample pdExamle = new PurchaseDetailPoExample();
            PurchaseDetailPoExample.Criteria criteria = pdExamle.createCriteria();
            criteria.andPurchaseIdEqualTo(id);
            purchaseDetailPoMapper.deleteByExample(pdExamle);
            return "删除成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败";
        }
    }

    //提交采购单
    public Integer updatePurchaseStatus(PurchasePo po){
        try {
            //提交
            po.setFixTime(new Date());
            po.setStatus(PurchaseStatus.UNREVIEWED.getStatus());
            po.setFixTime(new Date());
            purchasePoMapper.updateByPrimaryKeySelective(po);

            //添加一条消息至消息记录表
            MessagePo messagePo = new MessagePo();
            messagePo.setExchange("purchase_exchange");
            messagePo.setRoutingKey("purchase.examine");
            messagePo.setMessageContent(po.getFixPerson()+"-"+po.getId());
            messagePo.setStatus(MessageStatus.UNSUCESS.getStatus());
            messagePo.setRetryCount(10);
            messagePo.setVersion(1L);
            messagePoMapper.insert(messagePo);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    //计算采购单金额
    public void countMoney(Long purNo){
        PurchasePoExample poExample = new PurchasePoExample();
        PurchasePoExample.Criteria criteria = poExample.createCriteria();
        criteria.andIdEqualTo(purNo);


        PurchaseDetailPoExample detailPoExample = new PurchaseDetailPoExample();
        PurchaseDetailPoExample.Criteria criteria1 = detailPoExample.createCriteria();
        criteria1.andPurchaseIdEqualTo(purNo);
        List<PurchaseDetailPo> poList = purchaseDetailPoMapper.selectByExample(detailPoExample);

        BigDecimal totalMoney = new BigDecimal(0);
        for (PurchaseDetailPo detailPo : poList) {
            totalMoney = totalMoney.add(detailPo.getTotalMoney());
        }
        //修改金额
        PurchasePo purchasePo = new PurchasePo();
        purchasePo.setTrueTotal(totalMoney);
        purchasePoMapper.updateByExampleSelective(purchasePo,poExample);
    }

    //修改采购单状态
    public void update(Long id,String status) throws Exception{
        PurchasePo purchasePo = new PurchasePo();
        purchasePo.setId(id);
        purchasePo.setStatus(status);
        purchasePoMapper.updateByPrimaryKeySelective(purchasePo);
    }

}
